package models

import (
	"rongyy/src/app"
	"rongyy/src/middlewares"
)

type HisApiLog struct {
	BaseModel  `xorm:"extends"`
	JsonInput  string `json:"json_input" xorm:"json"`
	Address    string `json:"address" xorm:"address"`
	Type       string `json:"type"`
	StatusCode int    `json:"status_code"`
	ErrorMsg   string `json:"error_msg"`
	JsonOutput string `json:"json_output" xorm:"json"`
}

type HisApiLogReq struct {
	PatientId  string `json:"patient_id"`
	Jylsh      string `json:"jylsh"`
	Limit      int    `json:"limit"`
	Offset     int    `json:"offset"`
	Type       string `json:"type"`
	StatusCode int    `json:"status_code"`
}

func (oot *HisApiLog) TableName() string {
	return "his_api_log"
}

func (oot *HisApiLog) Insert() error {
	_, err := app.DB().Insert(oot)
	if err != nil {
		middlewares.Logger.Println("ERROR - HisApiLog Insert failed ", err)
		//return errors.New("保存失败")
	}
	return nil
}

func (oot *HisApiLog) Query(params HisApiLogReq) ([]HisApiLog, int64, error) {
	data := make([]HisApiLog, 0)
	s := app.DB().Table("his_api_log")
	if params.StatusCode != 200 {
		s = s.Where("status_code != 200")
	}
	total, err := s.Limit(params.Limit, params.Offset).Desc("created_at").FindAndCount(&data)
	if err != nil {
		return nil, 0, err
	}
	return data, total, nil
}
